package com.cy.store.controller;

import com.cy.store.entity.Address;
import com.cy.store.service.IAddressService;
import com.cy.store.service.impl.AddressServiceImpl;
import com.cy.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addresses")
public class AddressController extends BaseController {
    @Autowired
    private IAddressService addressService;

    /**
     * 添加地址
     *
     * @param address
     * @param session
     * @return
     */
    @RequestMapping("/add_new_address")
    public JsonResult<Void> addNewAddress(Address address, HttpSession session) {
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);

        //调用业务层方法
        addressService.addNewAddress(uid, username, address);
        //相应成功
        return new JsonResult<Void>(OK);
    }

    /**
     * 显示收货地址
     *
     * @param session
     * @return
     */
    @RequestMapping({"", "/"})
    public JsonResult<List<Address>> getByUid(HttpSession session) {
        Integer uid = getUidFromSession(session);
        List<Address> list = addressService.getByUid(uid);
        return new JsonResult<>(OK, list);
    }

    /**
     * 设置默认地址
     *
     * @param aid
     * @param session
     * @return
     */
    @RequestMapping("/set_default/{aid}")
    public JsonResult<Void> setDefault(@PathVariable("aid") Integer aid, HttpSession session) {
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        addressService.setDefault(aid, uid, username);
        return new JsonResult<>(OK);
    }

    /**
     * 删除地址
     *
     * @param aid
     * @param session
     * @return
     */
    @RequestMapping("/delete/{aid}")
    public JsonResult<Void> delete(@PathVariable("aid") Integer aid, HttpSession session) {
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        addressService.delete(aid, uid, username);
        return new JsonResult<>(OK);
    }

    /**
     * 修改收货地址
     *
     * @param aid
     * @param address
     * @param session
     * @return
     */
    @RequestMapping("/update/{aid}")
    public JsonResult<Void> update(@PathVariable("aid") Integer aid, Address address, HttpSession session) {
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);

        addressService.update(aid, uid, address, username);
        return new JsonResult<>(OK);
    }

    /**
     * 获取收货信息
     *
     * @param aid
     * @return
     */
    @RequestMapping("/get/{aid}")
    public JsonResult<Address> getByAid(@PathVariable("aid") Integer aid, HttpSession session) {
        Address address = addressService.findByAid(aid, getUidFromSession(session));
        return new JsonResult<>(OK, address);
    }

    /**
     * 获取省和市的code
     *
     * @return
     */
    @RequestMapping("/get_code/{aid}")
    public JsonResult<Address> getCode(@PathVariable("aid") Integer aid, HttpSession session) {
        Address result = addressService.findByAid(aid, getUidFromSession(session));
        //重新封装Address对象 只存放provinceCode和cityCode
        Address address = new Address();
        address.setProvinceCode(result.getProvinceCode());
        address.setCityCode(result.getCityCode());
        return new JsonResult<>(OK, address);
    }
}
